package com.ht.medicaltreatment.activity

import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ActivityUtils
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.ReceiveListAdapter
import com.ht.medicaltreatment.bean.YangBenJieShouBeanRfidSample
import com.ht.medicaltreatment.view.AddressListDialog
import com.ht.medicaltreatment.view.UserNameDialog
import com.ht.medicaltreatment.viewmodel.ReceiveBuildViewModel
import com.xgst.lib.utils.TimeUtils
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_build_layout.btnNext
import kotlinx.android.synthetic.main.activity_build_layout.cbBG
import kotlinx.android.synthetic.main.activity_build_layout.cbBY
import kotlinx.android.synthetic.main.activity_build_layout.cbCW
import kotlinx.android.synthetic.main.activity_build_layout.cbQT
import kotlinx.android.synthetic.main.activity_build_layout.etJieShou
import kotlinx.android.synthetic.main.activity_build_layout.etJieShouRen
import kotlinx.android.synthetic.main.activity_build_layout.etOther
import kotlinx.android.synthetic.main.activity_build_layout.etRemark
import kotlinx.android.synthetic.main.activity_build_layout.etType
import kotlinx.android.synthetic.main.activity_build_layout.imgAddress
import kotlinx.android.synthetic.main.activity_build_layout.imgJieShou
import kotlinx.android.synthetic.main.activity_build_layout.imgZhuanSongRen
import kotlinx.android.synthetic.main.activity_build_layout.mSwitch
import kotlinx.android.synthetic.main.activity_build_layout.recyclerView
import kotlinx.android.synthetic.main.activity_build_layout.titleJ
import kotlinx.android.synthetic.main.activity_build_layout.tvCaixue
import kotlinx.android.synthetic.main.activity_build_layout.tvCount
import kotlinx.android.synthetic.main.activity_build_layout.tvFormName
import kotlinx.android.synthetic.main.activity_build_layout.tvTime
import kotlinx.android.synthetic.main.activity_build_layout.tvZhuanSongRen

class SampleReceiveBuildActivity:BaseActivity(), View.OnClickListener {
    private lateinit var userDialog:UserNameDialog
    private val viewModel by viewModels<ReceiveBuildViewModel>()
    private var addressList:ArrayList<String>? = arrayListOf()
    //是否同一个人
    private var isSamplePerson = false
    //转送人输入框是第一个输入的吗
    private var isZhuanSongRen = false
    private var list:ArrayList<YangBenJieShouBeanRfidSample>? = null

    override fun getLayoutId() = R.layout.activity_build_layout

    override fun isViewStup() = true

    override fun init() {
        setTitle("创建接收表单")
        userDialog = UserNameDialog(this)
        tvTime.text = TimeUtils.getCurrentTime()

        //获取传递过来的数据
        list = intent.getParcelableArrayListExtra("list")
        addressList = intent.getStringArrayListExtra("addressList")
        val isStanding = intent.getBooleanExtra("isStanding",false)

        //配送条件
        val deliveryCondition = intent.getStringExtra("deliveryCondition")?:"";
        //采血地点
        val drawBloodPlace = intent.getStringExtra("drawBloodPlace");
        //接收地点
        val receivingPlace = intent.getStringExtra("receivingPlace");
        //血样类型
        val bloodSource = intent.getStringExtra("bloodSource");

        tvCaixue.text = drawBloodPlace
        etJieShou.setText(receivingPlace)
        etType.setText(bloodSource)

        if(deliveryCondition.contains("常温")){
            cbCW.isChecked = true
        }
        if(deliveryCondition.contains("冰上")){
            cbBY.isChecked = true
        }
        if(deliveryCondition.contains("避光")){
            cbBG.isChecked = true
        }
        if(deliveryCondition.contains("其他")){
            cbQT.isChecked = true
        }

        val adapter = ReceiveListAdapter(isStanding)
        if(isStanding){
            titleJ.visibility = View.VISIBLE
        }
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        adapter.setNewInstance(list)
        tvFormName.text = "临床试验转运接收表${TimeUtils.getCurrentTime("yyyyMMddHHmmss")}"

        tvCount.text = "${list?.size}"

        initClick()
    }

    private fun initClick() {
        tvZhuanSongRen.setOnClickListener(this)
        imgZhuanSongRen.setOnClickListener(this)
        etJieShouRen.setOnClickListener(this)
        imgJieShou.setOnClickListener(this)
        tvCaixue.setOnClickListener(this)
        imgAddress.setOnClickListener(this)
        btnNext.setOnClickListener(this)

        mSwitch.setOnCheckedChangeListener { buttonView, isChecked ->
            isSamplePerson = isChecked
            if(isChecked){
                val zhuanSong = tvZhuanSongRen.text.toString()
                val jieShouRen = etJieShouRen.text.toString()
                if(zhuanSong.isEmpty() && jieShouRen.isNotEmpty()){
                    tvZhuanSongRen.text = jieShouRen
                }else if(zhuanSong.isNotEmpty() && jieShouRen.isEmpty()){
                    etJieShouRen.text = zhuanSong
                }
            }else{
                if(isZhuanSongRen){
                    etJieShouRen.text = ""
                }else{
                    tvZhuanSongRen.text = ""
                }
            }
        }
    }

    override fun onClick(v: View?) {
        when(v){
            //转送人
            imgZhuanSongRen,tvZhuanSongRen -> userDialog.show { account, psw ->
                   viewModel.getUserName(account,psw){
                       isZhuanSongRen = true
                       tvZhuanSongRen.text = it
                       if(isSamplePerson){
                           etJieShouRen.text = it
                       }
                   }
                }
            //接收人
            etJieShouRen,imgJieShou -> userDialog.show { account, psw ->
                viewModel.getUserName(account,psw){
                    etJieShouRen.text = it
                    isZhuanSongRen = false
                    if(isSamplePerson){
                        tvZhuanSongRen.text = it
                    }
                }
            }
            //采血地点
            tvCaixue,imgAddress -> {
                if(addressList == null)return
                AddressListDialog(this).show(addressList!!){
                    tvCaixue.text = it
                }
            }
            //确定
            btnNext -> {
                val caiXue = tvCaixue.text.toString()
                if(caiXue.isEmpty()){
                    showToast("请选择采血地点")
                    return
                }

                val jieShou = etJieShou.text.toString()
                if(jieShou.isEmpty()){
                    showToast("请输入接收地点")
                    return
                }

                val zhuanSong = tvZhuanSongRen.text.toString()
                if(zhuanSong.isEmpty()){
                    showToast("请输入转送人")
                    return
                }

                val jieShouRen = etJieShouRen.text.toString()
                if(jieShouRen.isEmpty()){
                    showToast("请输入接收人")
                    return
                }

                val type = etType.text.toString()
                if(type.isEmpty()){
                    showToast("请输入样本类型")
                    return
                }

                var cb:String? = null
                if(cbCW.isChecked){
                    cb = "常温"
                }
                if(cbBY.isChecked){
                    if(cb == null){
                        cb = "冰浴"
                    }else{
                        cb = "$cb,冰浴"
                    }
                }
                if(cbBG.isChecked){
                    if(cb == null){
                        cb = "避光"
                    }else{
                        cb = "$cb,避光"
                    }
                }
                if(cbQT.isChecked){
                    if(etOther.text.toString().isEmpty()){
                        showToast("请输入其他内容")
                        return
                    }
                    if(cb == null){
                        cb = etOther.text.toString()
                    }else{
                        cb = "$cb,${etOther.text}"
                    }
                }
                if(cb == null){
                    showToast("请选择配送条件")
                    return
                }

                val map = mutableMapOf<String,String>()
                //表单名称
                map["tableName"] = tvFormName.text.toString()
                //采血地点
                map["bloodCollectingPlace"] = caiXue
                //接收地点
                map["receivingPlace"] = jieShou
                //转运人
                map["transportPersonName"] = zhuanSong
                //接收人
                map["receivePersonName"] = jieShouRen
                //转运条件
                map["transportCondition"] = cb
                //时间
                map["receiveTime"] = tvTime.text.toString()
                //样本类型
                map["bloodSource"] = type
                //备注
                map["remark"] = etRemark.text.toString()
                var ids:String? = null
                list?.forEach {
                    if(ids == null){
                        ids = "${it.id}"
                    }else{
                        ids = "$ids,${it.id}"
                    }
                }
                if(ids == null){
                    showToast("没有试管数据")
                    return
                }
                //试管id 逗号隔开
                map["idList"] = ids!!
                showProgress()
                viewModel.submit(map,{
                    dismissProgress()
                    showToast("提交成功")
                    //成功之后，跳转到离心机页面（2024.4.26）
                    startActivity(CentrifugeActivity::class.java)
                    ActivityUtils.finishActivity(SampleReceiveListActivity::class.java)
                    finish()
                },{
                    dismissProgress()
                    showToast(it)
                })
            }
        }
    }
}
